﻿using NModBusDrive;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace TestModBus
{

    public class BussinessModel
    {
        #region 属性
        public string COM
        {
            get { return ConfigurationManager.AppSettings["COM"]; }
        }
        /// <summary>
        /// 射频从站地址
        /// </summary>
        public byte SlaveShePingAddress
        {
            get { return Convert.ToByte(ConfigurationManager.AppSettings["slaveShePingAddress"]); }
        }

        /// <summary>
        /// 射频开始写入地址
        /// </summary>
        public ushort StartShePingAddressWrite
        {
            get { return Convert.ToUInt16(ConfigurationManager.AppSettings["startShePingAddressWrite"]); }
        }

        /// <summary>
        /// 射频开始读取地址
        /// </summary>
        public ushort StartShePingAddressRead
        {
            get { return Convert.ToUInt16(ConfigurationManager.AppSettings["startShePingAddressRead"]); }
        }

        /// <summary>
        /// 射频写入数据大小
        /// </summary>
        public ushort NumberShePingOfPointsWrite
        {
            get { return Convert.ToUInt16(ConfigurationManager.AppSettings["numberShePingOfPointsWrite"]); }
        }

        /// <summary>
        /// 射频读取数据大小
        /// </summary>
        public ushort NumberShePingOfPointsRead
        {
            get { return Convert.ToUInt16(ConfigurationManager.AppSettings["numberShePingOfPointsRead"]); }
        }

        /// <summary>
        /// 机械（PLC）从站地址
        /// </summary>
        public byte SlaveJiXieAddress
        {
            get { return Convert.ToByte(ConfigurationManager.AppSettings["slaveJiXieAddress"]); }
        }

        /// <summary>
        ///  机械（PLC）开始写入位置
        /// </summary>
        public ushort StartJiXieAddressWrite
        {
            get { return Convert.ToUInt16(ConfigurationManager.AppSettings["startJiXieAddressWrite"]); }
        }

        /// <summary>
        ///  机械（PLC）开始读取开始位置
        /// </summary>
        public ushort StartJiXieAddressRead
        {
            get { return Convert.ToUInt16(ConfigurationManager.AppSettings["startJiXieAddressRead"]); }
        }

        /// <summary>
        ///  机械（PLC）读取数据大小
        /// </summary>
        public ushort NumberJiXieOfPointsRead
        {
            get { return Convert.ToUInt16(ConfigurationManager.AppSettings["numberJiXieOfPointsRead"]); }
        }

        public int BtY0 { get; set; }
        public int BtY1 { get; set; }
        public int BtY2 { get; set; }
        public int BtY3 { get; set; }
        public int BtY4 { get; set; }
        public int BtY5 { get; set; }
        public int BtY6 { get; set; }
        public int BtY7 { get; set; }
        public int BtY10 { get; set; }
        public int BtY11 { get; set; }
        public int BtY12 { get; set; }
        public int BtY13 { get; set; }
        public int BtY14 { get; set; }
        public int BtY15 { get; set; }
        public int BtY16 { get; set; }
        public int BtY17 { get; set; }
        public int BtY20 { get; set; }
        public int BtCH1 { get; set; }
        public int BtCH2 { get; set; }
        public int BtCH3 { get; set; }
        public int BtCH4 { get; set; }
        public int BtCH5 { get; set; }
        public int BtCH6 { get; set; }
        public int BtCH7 { get; set; }
        public int BtCH8 { get; set; }
        public int BtCH9 { get; set; }
        public int BtCH10 { get; set; }
        public int BtCH11 { get; set; }
        public int BtCH12 { get; set; }
        public int BtReset { get; set; }
        #endregion

        /// <summary>
        /// 机械矩阵控制字
        /// </summary>
        public Dictionary<string, ushort> function;

        /// <summary>
        /// 写多个线圈
        /// </summary>
        public ushort[] RegisterBuffer { get; set; }

        public ILog Logger { get; set; }

        public string head1, head2, now_function;

        public SerialPortUtil serialPortUtil = new SerialPortUtil();
        public BussinessModel()
        {
            serialPortUtil.Inin(COM, SerialPortBaudRates.BaudRate_19200,
              System.IO.Ports.Parity.None, SerialPortDatabits.EightBits, System.IO.Ports.StopBits.One);
            serialPortUtil.OpenPort();

            function = new Dictionary<string, ushort>();
            function["CH1"] = 1;
            function["CH2"] = 2;
            function["CH3"] = 3;
            function["CH4"] = 4;
            function["CH5"] = 5;
            function["CH6"] = 6;
            function["CH7"] = 7;
            function["CH8"] = 8;
            function["CH9"] = 9;
            function["CH10"] = 10;
            function["CH11"] = 11;
            function["CH12"] = 12;
            function["回初位置"] = 20;
            function["上电"] = 21;
            function["下电"] = 22;
            function["故障复位"] = 23;

            head1 = "FFFF";
            head2 = "FFEE";
            RegisterBuffer = new ushort[17];
            for (var i = 0; i <17; i++)
            {
                RegisterBuffer[i] = 0;
            }
        }

        public ModeOperType operTypeBt { get; set; }

        /// <summary>
        /// 发送机械矩阵通道数据
        /// </summary>
        public void SendCh() 
        {
            string SendStr = string.Format("{0}{1}{2}{3}{4}{5}", head1, 
                PublicMethod.Int2Hex(function[now_function]), 0x0, 0x0, 0x0, 0x0);
            byte[] data = serialPortUtil.StrToToHexByte(SendStr);
            serialPortUtil.SendData(data, 0, data.Length);
            string _log = string.Format("【机械切换】[指令未转换]->二进制数据: {0} 十进制数据: {1} [指令转换16进制]: {2}", Convert.ToString((int)function[now_function], 2), (int)function[now_function], SendStr);
            Logger.Info(_log);
            LogHelper.Info(_log);
        }

        /// <summary>
        /// 发送射频矩阵数据
        /// </summary>
        public void SendShePin()
        {
            string SendStr = string.Format("{0}{1}{2}{3}{4}{5}", head2,
                PublicMethod.Int2Hex(RegisterBuffer.ConvertToDecimal()), 0x0 , 0x0, 0x0, RegisterBuffer[0]);
            byte[] data = serialPortUtil.StrToToHexByte(SendStr);
            serialPortUtil.SendData(data, 0, data.Length);
            string _log = string.Format("【射频切换】[指令未转换]->二进制数据: {0} 十进制数据: {1} [指令转换16进制]: {2}", RegisterBuffer.ConvertToString(), RegisterBuffer.ConvertToDecimal(), SendStr);
            Logger.Info(_log);
            LogHelper.Info(_log);
        }

    }


    public enum ColorType
    {
        Default = 1,
        Error,
        Success
    }

    public enum ModeOperType
    {
        Y0 = 1,
        Y1 = 2,
        Y2 = 3,
        Y3 = 4,
        Y4 = 5,
        Y5 = 6,
        Y6 = 7,
        Y7 = 8,
        Y10 = 9,
        Y11 = 10,
        Y12 = 11,
        Y13 = 12,
        Y14 = 13,
        Y15 = 14,
        Y16 = 15,
        Y17 = 16,
        Y20 = 17,
        CH1 = 18,
        CH2 = 19,
        CH3 = 20,
        CH4 = 21,
        CH5 = 22,
        CH6 = 23,
        CH7 = 24,
        CH8 = 25,
        CH9 = 26,
        CH10 = 27,
        CH11 = 28,
        CH12 = 29,
        ResetAll=31
    }


}
